Data Types and Variables
In this lesson, we will learn about data types and variables in Python.
We'll cover the following
Variables#
A variable is simply a name to which a value can be assigned.
The simplest way to assign a value to a variable is through the = operator. Variables allow us to store data so that we can use it later to perform operations in the code.
Data Types#
Python provides three main data types:
- Numbers
- Strings
- Booleans
Let’s cover these in detail below:
Floating point number#
Floating point numbers, or floats, refer to positive and negative numbers with a fractional part.
Complex numbers#
Python also supports complex numbers. There are two ways to create a complex number:
complex()is used to create complex numbers. The first argument is the real part and the second argument is the imaginary part.- Simply write the value as
x+yj, wherexis the real part andyis the complex part andjrepresents the iota.
Extracting real and imaginary#
- To extract the real part of a complex number, we use
.real.
z.real
- To extract the imaginary part of a complex number, we use
.imag.
z.imag
Let’s look at an example of this below:
Booleans#
The Boolean (also known as bool) data type allows us to choose between two values: true and false. In Python, we can simply use True or False to represent a bool:
Booleans are often used in data comparisons.
Strings#
A string is a collection of characters enclosed within single or double quotation marks.
Length of a string#
The length of a string can be found using the len() function.
Indexing#
A string in Python is indexed from 0 to n-1 where n is its length. This means that the index of the first character in a string is 0. Each character in a string can be accessed using its index. The index must be closed within square brackets, [].
We’ll learn about operators in the next lesson.
About This Course
Operators